package org.file5.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.file5.domain.Article;
import org.file5.domain.Attachment;
import org.file5.domain.Category;
import org.file5.domain.Person;
import org.file5.service.ServiceFascade;
import org.file5.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.googlecode.objectify.Key;

@Controller
@RequestMapping("/admin/editprofile")
@SessionAttributes("person")
public class EditProfileForm {
	private final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private ServiceFascade sf;
	@Autowired
	private UserService googleUserService;
	
	@InitBinder
	public void setAllowedFields(WebDataBinder dataBinder) {
		dataBinder.setDisallowedFields(new String[] { "uid" });
	}

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm( ModelMap model) {
		User googleUser = googleUserService.getCurrentUser();
		String personUid = sf.getPersonService().getPersonUid(googleUser);
		Person person = sf.getPersonService().find(personUid);
model.addAttribute("person", person);


		return "admin/profileedit";
	}

	@RequestMapping(method = { RequestMethod.PUT, RequestMethod.POST })
	public String processSubmit(@ModelAttribute("person") Person person,
			BindingResult result, SessionStatus status,
			HttpServletRequest request) {

		// new PetValidator().validate(article, result);
		if (result.hasErrors()) {
			return "admin/profileedit";
		} else {

			sf.getPersonService().updateProfile(person);
			return "redirect:/admin/home.htm";
		}
	}
}
